﻿/// <reference path="~/GeneratedArtifacts/viewModel.js" />

myapp.BrowseCustomers.Customers_postRender = function (element, contentItem) {
    // Write code here.
    lsWire.list.enableMultiSelect(contentItem);
};
myapp.BrowseCustomers.getSelected_execute = function (screen) {
    // Write code here.
    var list = screen.findContentItem("Customers");
    var count = lsWire.list.selectedCount(list);
    var selected = lsWire.list.selected(list);
    var text = "Customers Selected\n\n";
    _.forEach(selected, function (item) {
        text += item.CustomerID + " - " + item.CompanyName + "\n";
    });
    text += "\n\nCount = " + count;
    window.alert(text);
};
myapp.BrowseCustomers.SelectAll_render = function (element, contentItem) {
    // Write code here.
    lsWire.checkbox.render(element, contentItem);
 
    // When the checkbox changes state, either select all or not
    contentItem.dataBind("screen.SelectAll", function (newValue) {
        if (newValue != undefined) {
            var list = contentItem.screen.findContentItem("Customers");
            lsWire.list.selectAll(list, newValue);
        }
    });
};
myapp.BrowseCustomers.SetLimits_render = function (element, contentItem) {
    // Write code here.
    lsWire.checkbox.render(element, contentItem);

    // When the checkbox changes state, set or remove our limits
    contentItem.dataBind("screen.SetLimits", function (newValue) {

        if (newValue !== undefined) {

            // Our dependent item
            var selectAll = contentItem.screen.findContentItem("SelectAll");

            // Get our list
            var list = contentItem.screen.findContentItem("Customers");

            // Current count of selected items
            var count = lsWire.list.selectedCount(list);

            // If the checkbox is selected, limit to 4 items
            if (newValue != undefined && newValue === true) {

                // Set our limit, returns the limit count
                var ttl = lsWire.list.totalSelectionsAllowed(list, 4);

                // if select all was set, checked
                if (selectAll.value === true)

                    // set the property to false, dataBinding will do the rest
                    contentItem.screen.SelectAll = false;

                    // Else is the current count greater than our limit?
                else if (count > ttl) {

                    // Count is greater, so unselect all items
                    lsWire.list.selectAll(list, false);
                }

                // Since we are limiting, disable the select all checkbox
                selectAll.isEnabled = false;
            } else {

                // Limit was unselected, renable select all and remove limits
                selectAll.isEnabled = true;
                lsWire.list.totalSelectionsAllowed(list, null);
            }
        }
    });
};
myapp.BrowseCustomers.getSelected_postRender = function (element, contentItem) {
    // Write code here.

};